package com.java110.mall.cart.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.dao.IInoutOrderProductServiceDao;
import com.java110.core.base.dao.BaseServiceDao;
import com.java110.utils.constant.ResponseConstant;
import com.java110.utils.exception.DAOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 出入库商品服务 与数据库交互
 * Created by wuxw on 2017/4/5.
 */
@Service("inoutOrderProductServiceDaoImpl")
//@Transactional
public class InoutOrderProductServiceDaoImpl extends BaseServiceDao implements IInoutOrderProductServiceDao {

    private static Logger logger = LoggerFactory.getLogger(InoutOrderProductServiceDaoImpl.class);


    /**
     * 保存出入库商品信息 到 instance
     *
     * @param info bId 信息
     * @throws DAOException DAO异常
     */
    @Override
    public void saveInoutOrderProductInfo(Map info) throws DAOException {
        logger.debug("保存出入库商品信息Instance 入参 info : {}", info);

        int saveFlag = sqlSessionTemplate.insert("inoutOrderProductServiceDaoImpl.saveInoutOrderProductInfo", info);

        if (saveFlag < 1) {
            throw new DAOException(ResponseConstant.RESULT_PARAM_ERROR, "保存出入库商品信息Instance数据失败：" + JSONObject.toJSONString(info));
        }
    }


    /**
     * 查询出入库商品信息（instance）
     *
     * @param info bId 信息
     * @return List<Map>
     * @throws DAOException DAO异常
     */
    @Override
    public List<Map> getInoutOrderProductInfo(Map info) throws DAOException {
        logger.debug("查询出入库商品信息 入参 info : {}", info);

        List<Map> infos = sqlSessionTemplate.selectList("inoutOrderProductServiceDaoImpl.getInoutOrderProductInfo", info);

        return infos;
    }


    /**
     * 修改出入库商品信息
     *
     * @param info 修改信息
     * @throws DAOException DAO异常
     */
    @Override
    public void updateInoutOrderProductInfo(Map info) throws DAOException {
        logger.debug("修改出入库商品信息Instance 入参 info : {}", info);

        int saveFlag = sqlSessionTemplate.update("inoutOrderProductServiceDaoImpl.updateInoutOrderProductInfo", info);

        if (saveFlag < 1) {
            throw new DAOException(ResponseConstant.RESULT_PARAM_ERROR, "修改出入库商品信息Instance数据失败：" + JSONObject.toJSONString(info));
        }
    }

    /**
     * 查询出入库商品数量
     *
     * @param info 出入库商品信息
     * @return 出入库商品数量
     */
    @Override
    public int queryInoutOrderProductsCount(Map info) {
        logger.debug("查询出入库商品数据 入参 info : {}", info);

        List<Map> infos = sqlSessionTemplate.selectList("inoutOrderProductServiceDaoImpl.queryInoutOrderProductsCount", info);
        if (infos.size() < 1) {
            return 0;
        }

        return Integer.parseInt(infos.get(0).get("count").toString());
    }

    @Override
    public int saveInoutOrderProducts(Map info) {
        logger.debug("saveInoutOrderProducts 入参 info : {}", info);

        int saveFlag = sqlSessionTemplate.insert("inoutOrderProductServiceDaoImpl.saveInoutOrderProducts", info);
        return saveFlag;
    }

    /**
     * 查询进出数量
     * @param info
     * @return
     */
    @Override
    public List<Map> queryProductInCount(Map info) {
        logger.debug("查询queryProductInoutCount信息 入参 info : {}", info);

        List<Map> infos = sqlSessionTemplate.selectList("inoutOrderProductServiceDaoImpl.queryProductInCount", info);

        return infos;
    }

    /**
     * 查询进出数量
     * @param info
     * @return
     */
    @Override
    public List<Map> queryProductOutCount(Map info) {
        logger.debug("查询queryProductInoutCount信息 入参 info : {}", info);

        List<Map> infos = sqlSessionTemplate.selectList("inoutOrderProductServiceDaoImpl.queryProductOutCount", info);

        return infos;
    }



}
